from backend.utils import Interp


Table16 = (
(25,   0.5, 0.5, 0.5, 1.3, 1.3, 1.3),
(50,   0.6, 0.9, 1.2, 1.5, 1.7, 1.9),
(100,  0.7, 1.0, 1.4, 1.8, 2.0, 2.2),
(125,  0.8, 1.1, 1.5, 1.9, 2.1, 2.4),

(150,  0.8, 1.1, 1.6, 2.0, 2.2, 2.5),
(200,  1.0, 1.4, 2.0, 2.5, 2.8, 3.2),
(250,  1.3, 1.8, 2.5, 3.2, 3.6, 4.0),
(300,  1.6, 2.2, 3.2, 4.0, 4.5, 5.0),

(400,  2.0, 2.8, 4.0, 5.0, 5.6, 6.3),
(600,  3.2, 4.5, 6.3, 8.0, 9.6, 10.0),
(800,  4.0, 5.6, 8.0, 10.0,11.0,12.5),
(1000, 5.0, 7.1, 10.0, 12.5, 14.0, 16.0),
)


def Table16_Process(pollution_class, 
              material, 
              multiple_num, 
              peak_voltage, 
              ):

    if pollution_class == 0: # 污染等级1
        return "选用合适表格中的电气间隙"

    if pollution_class == 1: # 污染等级2
        if material == 0:
            CDidx = 1
        elif material == 1:
            CDidx = 2
        else:
            CDidx = 3
    elif pollution_class == 2: # 污染等级3
        if material == 0:
            CDidx = 4
        elif material == 1:
            CDidx = 5
        else:
            CDidx = 6

    if peak_voltage <= Table16[0][0]:
        CreepDist = Table16[0][CDidx]
    for i in range(1, len(Table16)):
        if peak_voltage > Table16[i-1][0] and peak_voltage <= Table16[i][0]:
            CreepDist = Interp(Table16[i-1][0], Table16[i][0],  
                               Table16[i-1][CDidx], Table16[i][CDidx],
                               peak_voltage)
            break
    
    if multiple_num == 1:
        CreepDist = CreepDist*2

    return CreepDist



